package com.hkpw.system.utils;


import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.hkpw.hik.ArtemisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @Auther: andy
 * @Date: 2022/9/29 14:33
 * @Description: HkApiUtils 海康人脸平台接口工具类
 * @Version 1.0.0
 * <p>
 * host: 172.16.3.102:443
 * appKey: 20487502
 * appSecret: mRNeYyz67q0Sx8E91ltr
 */
@Slf4j
@Component
public class HkFaceApiUtils {

    /**
     * host
     */
    public static String host;

    /**
     * appKey
     */

    public static String appKey;

    /**
     * appSecret
     */
    public static String appSecret;

    @Value("${hikvision.face.appKey}")
    public void setAppKey(String appKey) {
        HkFaceApiUtils.appKey = appKey;
    }

    @Value("${hikvision.face.appSecret}")
    public void setAppSecret(String appSecret) {
        HkFaceApiUtils.appSecret = appSecret;
    }

    @Value("${hikvision.face.host}")
    public void setHost(String host) {
        HkFaceApiUtils.host = host;
    }

    public static String getAppKey() {
        return appKey;
    }

    public static String getAppSecret() {
        return appSecret;
    }

    public static String getHost() {
        return host;
    }

    /**
     * 获取海康接口地址信息
     *
     * @param path
     * @return
     */
    public static Map<String, String> getHkUrlPath(String path) {
        Map<String, String> URL = new HashMap<String, String>(16);
        //查询订阅事件的URL
        URL.put("https://", path);
        return URL;
    }

    /**
     * 海康接口调用公共方法
     *
     * @return
     */
    public static JSONObject callPostApiCommonFun(String path, Object params) {
        log.info("[HK接口请求]HkFaceApiUtils.callPostApiCommonFun(path={},params={})", path, params);
        JSONObject jsonResult = callPostApiCommonFunWithoutLog(path, params);
        return jsonResult;
    }

    /**
     * 海康接口调用公共方法
     *
     * @return
     */
    public static JSONObject callPostApiCommonFunWithoutLog(String path, Object params) {
        log.info("[HK接口请求]HkFaceApiUtils.callPostApiCommonFun(path={})", path);
        String body = null;
        if (Objects.nonNull(params)) {
            body = JSON.toJSON(params).toString();
        }
        String result = ArtemisUtil.callPostApiCommonFun(host, appKey, appSecret, path, body);
        JSONObject jsonResult = JSON.parseObject(result);
        log.info("[HK接口请求]HkFaceApiUtils.callPostApiCommonFun返回结果jsonResult={}", jsonResult);
        return jsonResult;
    }


}
